import QRCode from 'qrcode';
import { EventBus } from '@/utils/eventBus';
export default {
  data() {
    return {
      // ====== 上传文件的参数 start ======
      fileName: '', //文件名称
      importDialogVisible: false, //导入弹窗
      fileList: [], //导入的文件
      isSelectedFile: false, //是否选中文件
      fileSizeMB: 0, //上传的文件尺寸
      // ====== 上传文件的参数 end ======
      // ====== 支付相关参数 start =====
      payType: 'duigong', //weixin duigong
      menmberType: '', //购买会员类型
      payVisilbeDialog: false, //支付弹窗
      parParams: {
        menmberType: '',
        qrCodeUrl: '', //二维码图片地址
        codeUrl: '', //支付二维码
        orderNo: '' //订单编号
      },
      // ====== 支付相关参数 end =====
      // ====== 定时器相关参数 start =====
      countdown: 0, // 倒计时秒数
      timer: null, // 定时器对象
      totalTime: 60, // 总倒计时时间
      // ====== 定时器相关参数 end =====
      wxPayVisibleDialog: false //微信支付弹窗
    };
  },
  beforeDestroy() {
    this.clearSetInterval();
  },
  computed: {
    headers() {
      return {
        Authorization: this.$store.state.token
      };
    },
    uploadFileUrl() {
      return 'orderPlusApi/' + this.$url.uploadCorporateTransfer;
    }
  },
  methods: {
    closeDialogWxPayDialog() {
      this.clearSetInterval();
      this.wxPayVisibleDialog = false;
      EventBus.$emit('call-method');
    },
    //开通VIP
    async openVip() {
      let res = await this.$api.get(this.$url.queryUserInfo);
      if (res.code == 200) {
        this.userInfo = res.data;
        this.payVisilbeDialog = true;
      }
    },
    //获取支付二维码
    async getPayQrcode() {
      if (this.menmberType != this.parParams.menmberType) {
        this.parParams.qrCodeUrl = '';
      }
      if (!this.parParams.qrCodeUrl) {
        let params = {
          subscribeType: this.menmberType == 'year' ? 2 : 1
        };
        let res = await this.$api.post(this.$url.initWechatPayWeb, params);
        if (res.code == 200) {
          this.wxPayVisibleDialog = true;
          this.parParams = {
            ...res.data,
            menmberType: this.menmberType,
            qrCodeUrl: await QRCode.toDataURL(res.data.codeUrl)
          };
          this.clearSetInterval();
          this.timer = setInterval(() => {
            this.queryPayOrder();
          }, 1000);
        }
      } else {
        this.wxPayVisibleDialog = true;
      }
    },
    //清除定时器
    clearSetInterval() {
      clearInterval(this.timer);
      this.timer = null;
    },
    //查询订单状态
    async queryPayOrder() {
      let res = await this.$api.get(this.$url.queryPayOrder, { orderNo: this.parParams.orderNo });
      if (res.code == 200 && res.data.status == 2) {
        this.$message.success('支付成功');
        this.closeDialog();
        this.closeDialogWxPayDialog();
        this.clearSetInterval();
        if (this.$route.path != '/layout/payOrder') {
          this.goPage2('/layout/payOrder', '6-2');
        } else {
          EventBus.$emit('call-method');
        }
      }
    },
    changeType(type) {
      if (this.userInfo.isAlreadyTrial === '1' && type === 'month') {
        return this.$message.warning('体验会员只能购买一次哦');
      } else {
        if (this.menmberType != type) {
          this.menmberType = type;
        }
      }
    },
    // ====== 上传文件的方法 start ======
    //关闭上传转账截图的弹窗
    closeUploadDialog() {
      this.importDialogVisible = false;
      this.handleRemoveFile();
    },
    //上传文件成功
    async uploadFileSucess(file) {
      let importParams = {
        screenShot: file.data.url,
        subscribeType: this.menmberType === 'month' ? '1' : '2',
        amount: 1
      };
      let res = await this.$api.post(this.$url.corporateTransferAdd, importParams);
      if (res.code == 200) {
        this.$message.success('上传成功，请等待审核');
        this.closeDialog();
        this.closeUploadDialog();
      }
    },
    //删除文件时出发此方法
    handleRemoveFile() {
      this.$refs.upload.clearFiles();
      this.fileLis = [];
      this.fileName = '';
      this.isSelectedFile = false;
    },
    //文件状态改变触发此方法
    changeUploadFile(file, fileList) {
      this.fileList = fileList;
      const fileSize = file.size; // 单位：字节 (Bytes)
      const fileSizeKB = Math.round(fileSize / 1024); // 转 KB
      const fileSizeMB = (fileSizeKB / 1024).toFixed(2); // 转 MB
      this.fileSizeMB = fileSizeMB;
      this.fileName = file.name;
      this.isSelectedFile = true;
    },
    //确认上传文件
    confirmUpload() {
      if (this.fileList.length == 0) {
        this.$message.warning('请上传文件');
      } else {
        this.$refs.upload.submit(); //上传文件
      }
    }
    // ====== 上传文件的方法 end
  }
};
